﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataTienda
{
    public class LogError
    {
        private DateTime errorDt;

        private string src;

        private Exception errorInfo;

        public static string strDirectoryPath;

        public DateTime ErrorDate
        {
            get
            {
                DateTime dateTime = this.errorDt;
                return dateTime;
            }
            set
            {
                this.errorDt = value;
            }
        }

        public Exception ErrorInformation
        {
            get
            {
                Exception exception = this.errorInfo;
                return exception;
            }
            set
            {
                this.errorInfo = value;
            }
        }

        public string ErrorSrc
        {
            get
            {
                string str = this.src;
                return str;
            }
            set
            {
                this.src = value;
            }
        }

        static LogError()
        {
            LogError.strDirectoryPath = ConfigurationSettings.AppSettings["PathLog"];
        }

        public LogError()
        {
        }

        public static void Log_Err(string strErrorSource, Exception Ex)
        {
            LogError errInfo = new LogError();
            errInfo.ErrorDate = DateTime.Now;
            errInfo.ErrorSrc = strErrorSource;
            errInfo.ErrorInformation = Ex;
            LogError.LogErr(errInfo);
        }

        public static void LogErr(LogError errorDTO)
        {
            try
            {
                string directoryPath = LogError.strDirectoryPath;
                bool flag = string.IsNullOrEmpty(LogError.strDirectoryPath);
                if (!flag)
                {
                    string[] str = new string[] { directoryPath, "\\ErrorLog_", null, null, null, null };
                    DateTime now = DateTime.Now;
                    int year = now.Year;
                    str[2] = year.ToString();
                    now = DateTime.Now;
                    year = now.Month;
                    str[3] = year.ToString();
                    now = DateTime.Now;
                    year = now.Day;
                    str[4] = year.ToString();
                    str[5] = ".txt";
                    string path = string.Concat(str);
                    StreamWriter swErrorLog = null;
                    DirectoryInfo dtDirectory = null;
                    flag = Directory.Exists(directoryPath);
                    if (!flag)
                    {
                        dtDirectory = Directory.CreateDirectory(directoryPath);
                        dtDirectory = null;
                    }
                    flag = !File.Exists(path);
                    if (flag)
                    {
                        swErrorLog = new StreamWriter(path, true);
                        swErrorLog.WriteLine(string.Concat("Date and Time of Exception: ", errorDTO.ErrorDate));
                        swErrorLog.WriteLine(string.Concat("Source of Exception: ", errorDTO.ErrorSrc));
                        swErrorLog.WriteLine(" ");
                        swErrorLog.WriteLine(string.Concat("Error Message: ", errorDTO.ErrorInformation));
                        swErrorLog.WriteLine("------------------------------------------- ");
                        swErrorLog.WriteLine(" ");
                        swErrorLog.Close();
                        swErrorLog = null;
                    }
                    else
                    {
                        swErrorLog = new StreamWriter(path, true);
                        swErrorLog.WriteLine(string.Concat("Date and Time of Exception: ", errorDTO.ErrorDate));
                        swErrorLog.WriteLine(string.Concat("Source of Exception: ", errorDTO.ErrorSrc));
                        swErrorLog.WriteLine(" ");
                        swErrorLog.WriteLine(string.Concat("Error Message: ", errorDTO.ErrorInformation));
                        swErrorLog.WriteLine("------------------------------------------- ");
                        swErrorLog.WriteLine(" ");
                        swErrorLog.Close();
                        swErrorLog = null;
                    }
                }
            }
            catch (NullReferenceException nullReferenceException)
            {
                throw;
            }
        }
    }
}
